//
//  UserListViewController.m
//  豆瓣终极版
//
//  Created by dllo on 15/12/18.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "UserListViewController.h"
#import "MovieCollectViewController.h"
#import "LandPageViewController.h"
#import "AppTools.h"
@interface UserListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *userTableView;
@end

@implementation UserListViewController

- (void)dealloc{
    [_userTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(landBtnAction:)];
    
    self.userTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.userTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.userTableView];
    
    self.userTableView.delegate = self;
    self.userTableView.dataSource = self;
    [_userTableView release];
    
    // 注册cell
    [self.userTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
}

// 在view将要出现的时候判断是登陆还是注销
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"state"];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"state"] isEqualToString:@"YES"]) {
        self.navigationItem.rightBarButtonItem.title = @"注销";
    } else {
        self.navigationItem.rightBarButtonItem.title = @"登陆";

    }
}


// 点击登陆按钮触发事件
- (void)landBtnAction:(UIBarButtonItem *)landBtn{
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"state"] isEqualToString:@"YES"]) {
        // 添加提示框
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canelAlert = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            self.navigationItem.rightBarButtonItem.title = @"注销";
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"state"];
            
        }];
        [alertC addAction:canelAlert];
        UIAlertAction *confirmAlert = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.navigationItem.rightBarButtonItem.title = @"登陆";
            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"state"];
            

                   }];
        [alertC addAction:confirmAlert];
        [self presentViewController:alertC animated:YES completion:^{
                   }];
        
        

    } else {
        // 模态到登陆页面
        LandPageViewController *landPageVC = [[LandPageViewController alloc] init];
        UINavigationController *landNaC = [[UINavigationController alloc] initWithRootViewController:landPageVC];
        [self presentViewController:landNaC animated:YES completion:^{
            
            
        }];

    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";

    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"清除缓存";

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        MovieCollectViewController *movieCollectVC = [[MovieCollectViewController alloc] init];
        [self.navigationController pushViewController:movieCollectVC animated:YES];
        
    } if (indexPath.row == 2) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清除缓存" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:cancelAction];
        
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            // 清除缓存
            NSString *filePath = [AppTools createFilePathFromDocumentWithFolders:@[@"Movie", @"MovieList"] fileName:@"MovieList.txt"];
            NSString *filePathDetail = [AppTools createFilePathFromDocumentWithFolders:@[@"Movie", @"MovieDetail"] fileName:self.movieId];
            if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
                BOOL result = [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
                if (result) {
                    NSLog(@"删除成功");
                } else {
                    NSLog(@"该文件不存在");
                }
            } if ([[NSFileManager defaultManager] fileExistsAtPath:filePathDetail]) {
                BOOL result = [[NSFileManager defaultManager] removeItemAtPath:filePathDetail error:nil];
                if (result) {
                    NSLog(@"删除成功");
                } else {
                    NSLog(@"该文件不存在");
                }

            }
            
        }];
        [alertC addAction:confirmAction];
        

        
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
